/*     */ package org.poebot.hal;
/*     */ 
/*     */ import java.io.BufferedReader;
/*     */ import java.io.BufferedWriter;
/*     */ import java.io.File;
/*     */ import java.io.IOException;
/*     */ import java.io.InputStreamReader;
/*     */ import java.io.OutputStreamWriter;
///*     */ import java.io.PrintStream;
/*     */ import java.net.Socket;
/*     */ import java.util.ArrayList;
/*     */ import java.util.Random;
import org.apache.log4j.Logger;
/*     */ 
/*     */ public class Chatbot extends Thread
/*     */ {
///*     */   private static final String helpMessage = "Commands are: HELP | JOIN <CHANNEL> | NICK <NICK> | SPEAK | QUIET | VERSION | LOADIGNORE | LEASH <TIME> | DIE \r\n";
/*     */   private BufferedWriter writer;
/*     */   private BufferedReader reader;
/*     */   private Socket socket;
/*     */   private String server;
/*     */   private String channel;
/*     */   private String nick;
/*     */   private boolean changeServers;
/* 658 */   private boolean canTalk = true;
/*     */ 
/* 660 */   private boolean speakAtWill = false;
/*     */ 
/* 662 */   private long speakingDelay = 60000L;
/*     */ 
/* 664 */   private BotPrompter botPrompter = null;
/*     */ 
/* 666 */   private boolean learnFromChat = true;
/*     */ 
/* 668 */   private boolean isLeashed = true;
/*     */   private boolean hisMastersVoice;
/* 672 */   private static String version = "chatbot is made of love and rape.";
/*     */   private UltraHal hal;
/* 675 */   private Object halLock = new int[1];
/*     */ 
/* 677 */   private long lastSpokeTime = -1L;
/*     */ 
/* 679 */   private long leashTime = 0L;
/*     */ 
/* 681 */   private int pesterCount = 0;
/*     */ 
/* 683 */   private ArrayList<ChatbotListener> chatListeners = new ArrayList<ChatbotListener>();
/*     */ 
/* 685 */   private ArrayList<String> ignoreList = new ArrayList<String>();
/*     */ 
/* 687 */   private ArrayList<String> activeChannelList = new ArrayList<String>();
/*     */ 
/* 689 */   private static Logger logger = Logger.getLogger(Chatbot.class);
/*     */ 
/*     */   public Chatbot()
/*     */   {
/*  16 */     this.reader = null;
/*  17 */     this.writer = null;
/*  18 */     this.socket = null;
/*  19 */     this.changeServers = true;
/*  20 */     this.hisMastersVoice = false;
/*  21 */     this.isLeashed = true;
/*  22 */     this.leashTime = 1000L;
/*     */ 
/*  24 */     this.nick = generateNick();
/*  25 */     synchronized (this.halLock)
/*     */     {
/*  27 */       this.hal = new UltraHal();
/*     */     }
/*     */ 
/*  30 */     this.lastSpokeTime = System.currentTimeMillis();
/*     */   }
/*     */ 
/*     */   public void run()
/*     */   {
/*  44 */     int connection_attempts = 0;
/*     */ 
/*  46 */     newLineReceived("Starting bot...");
/*     */ 
/*  48 */     while ((!isInterrupted()) && (this.changeServers))
/*     */     {
/*  51 */       this.changeServers = false;
/*     */       try
/*     */       {
/*     */         do
/*     */         {
/*  56 */           connection_attempts++;
/*     */ 
/*  55 */           if (connectToServer(this.server, this.channel) >= 0) break; 
/*  55 */         }while (connection_attempts < 100);
/*     */       }
/*     */       catch (Exception e)
/*     */       {
/*  60 */         e.printStackTrace();
/*  61 */         return;
/*     */       }
/*     */ 
/*  64 */       MessageLoop();
/*     */     }
/*     */   }
/*     */ 
/*     */   public void setSever(String server)
/*     */   {
/*  70 */     this.server = server;
/*     */   }
/*     */ 
/*     */   public void setChannel(String channel)
/*     */   {
/*  75 */     this.channel = channel;
/*     */   }
/*     */ 
/*     */   public void setNick(String nick)
/*     */   {
/*  80 */     this.nick = nick;
/*     */   }
/*     */ 
/*     */   public String getSentence(String message)
/*     */   {
/*     */     String sentence;
/*  86 */     synchronized (this.halLock)
/*     */     {
/*  88 */       sentence = this.hal.getSentence(message);
/*     */     }
///*     */     String sentence;
/*  90 */     return sentence;
/*     */   }
/*     */ 
/*     */   public void trainOnSentence(String sentence)
/*     */   {
/*  95 */     if (sentence == null) {
/*  96 */       return;
/*     */     }
/*  98 */     synchronized (this.halLock)
/*     */     {
/* 100 */       this.hal.add(sentence);
/*     */     }
/*     */   }
/*     */ 
/*     */   public void loadBrain(File file)
/*     */   {
/* 106 */     synchronized (this.halLock)
/*     */     {
/* 108 */       this.hal.loadBrain(file);
/*     */     }
/*     */   }
/*     */ 
/*     */   public void saveBrain(File file)
/*     */   {
/* 114 */     synchronized (this.halLock)
/*     */     {
/* 116 */       this.hal.saveBrain(file);
/*     */     }
/*     */   }
/*     */ 
/*     */   public void setLearnFromChat(boolean learnFromChat)
/*     */   {
/* 123 */     this.learnFromChat = learnFromChat;
/*     */   }
/*     */ 
/*     */   public boolean addNameToIgnoreList(String nick)
/*     */   {
/* 128 */     synchronized (this.halLock)
/*     */     {
/* 130 */       nick = nick.toUpperCase();
/* 131 */       if (!this.ignoreList.contains(nick))
/*     */       {
/* 133 */         this.ignoreList.add(nick);
/* 134 */         return true;
/*     */       }
/*     */ 
/* 137 */       return false;
/*     */     }
/*     */   }
/*     */ 
/*     */   public void removeNameFromIgnoreList(String nick)
/*     */   {
/* 143 */     synchronized (this.halLock)
/*     */     {
/* 145 */       nick = nick.toUpperCase();
/* 146 */       this.ignoreList.remove(nick);
/*     */     }
/*     */   }
/*     */ 
/*     */   public void addChatListener(ChatbotListener listener)
/*     */   {
/* 152 */     this.chatListeners.add(listener);
/*     */   }
/*     */ 
/*     */   public void removeChatListener(ChatbotListener listener)
/*     */   {
/* 157 */     this.chatListeners.remove(listener);
/*     */   }
/*     */ 
/*     */   public void setChangeServer(boolean b)
/*     */   {
/* 162 */     this.changeServers = b;
/*     */   }
/*     */ 
/*     */   public void setSpeakAtWill(boolean speakAtWill)
/*     */   {
/* 169 */     this.speakAtWill = speakAtWill;
/* 170 */     synchronized (this.halLock)
/*     */     {
/* 172 */       if ((!speakAtWill) && (this.botPrompter != null))
/* 173 */         this.botPrompter.interrupt();
/* 174 */       this.botPrompter = null;
/*     */     }
/*     */   }
/*     */ 
/*     */   public void setSpeakingDelay(long speakingDelay)
/*     */   {
/* 180 */     if (this.speakingDelay == speakingDelay)
/* 181 */       return;
/* 182 */     this.speakingDelay = speakingDelay;
/* 183 */     synchronized (this.halLock)
/*     */     {
/* 185 */       if (this.botPrompter != null)
/*     */       {
/* 187 */         this.botPrompter.interrupt();
/* 188 */         if (this.speakAtWill)
/* 189 */           this.botPrompter = new BotPrompter(this, speakingDelay);
/*     */       }
/*     */     }
/*     */   }
/*     */ 
/*     */   void speakToChannels()
/*     */   {
/* 199 */     synchronized (this.halLock)
/*     */     {
/* 201 */       for (String channel : this.activeChannelList)
/* 202 */         writeToChannel(channel, this.hal.getSentence());
/*     */     }
/* 204 */     if (this.speakAtWill)
/*     */     {
/* 206 */       this.botPrompter = new BotPrompter(this, this.speakingDelay);
/* 207 */       this.botPrompter.start();
/*     */     }
/*     */     else
/*     */     {
/* 211 */       this.botPrompter = null;
/*     */     }
/*     */   }
/*     */ 
/*     */   private void MessageLoop()
/*     */   {
/* 228 */     String line = null;
/*     */     try
/*     */     {
/*     */       do
/*     */       {
/* 233 */         if (this.reader.ready())
/*     */         {
/* 235 */           line = this.reader.readLine();
/* 236 */           if (line == null) {
/*     */             break;
/*     */           }
/*     */           try
/*     */           {
/* 241 */             parseLine(line);
/*     */           }
/*     */           catch (Exception e)
/*     */           {
/* 245 */             logger.error(e);
/* 246 */             return;
/*     */           }
/*     */         }
/* 231 */         if (this.changeServers) break; 
/* 231 */       }while (!isInterrupted());
/*     */     }
/*     */     catch (Exception e)
/*     */     {
/* 253 */       logger.error(e);
/* 254 */       return;
/*     */     }
/* 256 */     if (this.changeServers)
/* 257 */       System.err.println("changing servers");
/*     */   }
/*     */ 
/*     */   private void parseLine(String line)
/*     */     throws IOException
/*     */   {
/* 270 */     String message = "";
/*     */ 
/* 272 */     String response = "";
/*     */ 
/* 275 */     boolean trained = false;
/*     */ 
/* 280 */     if (line.toLowerCase().startsWith("ping "))
/*     */     {
/* 282 */       this.writer.write("PONG " + line.substring(5) + "\r\n");
/* 283 */       this.writer.flush();
/* 284 */       return;
/*     */     }
/*     */     String sender;
///*     */     String sender;
/*     */     int endOfSender;
/* 287 */     if (line.startsWith(":"))
/*     */     {
/* 289 */       endOfSender = line.indexOf(" ");
/* 290 */       sender = line.substring(1, endOfSender);
/*     */     }
/*     */     else
/*     */     {
/* 294 */       sender = "*";
/* 295 */       endOfSender = 0;
/*     */     }
/*     */     String senderNick;
///*     */     String senderNick;
/* 297 */     if (sender.contains("!"))
/* 298 */       senderNick = sender.substring(0, sender.indexOf('!')).toUpperCase();
/*     */     else {
/* 300 */       senderNick = "*";
/*     */     }
/*     */ 
/* 305 */     if (line.substring(endOfSender).toLowerCase().startsWith(" privmsg"))
/*     */     {
/* 310 */       int startOfMessage = line.indexOf(":", endOfSender) + 1;
/* 311 */       message = line.substring(startOfMessage);
/* 312 */       String thisChannel = line.substring(endOfSender + " privmsg ".length(), line.indexOf(":", endOfSender) - 1);
/*     */ 
/* 315 */       if (thisChannel.compareTo(this.nick) == 0)
/*     */       {
/* 317 */         executeCommand(message, sender.substring(0, sender.indexOf("!")));
/*     */       }
/* 321 */       else if ((this.canTalk) && (message.toUpperCase().contains(this.nick.toUpperCase())) && (!this.ignoreList.contains(senderNick)))
/*     */       {
/* 323 */         if ((this.isLeashed) && (System.currentTimeMillis() - this.lastSpokeTime < this.leashTime))
/*     */         {
/* 325 */           this.pesterCount += 1;
/* 326 */           if (this.pesterCount == 4) {
/* 327 */             writeToChannel(thisChannel, "LEAVE ME ALONE");
/*     */           }
/*     */         }
/*     */         else
/*     */         {
/* 332 */           if (message.length() > this.nick.length() + 1)
/*     */           {
/* 334 */             synchronized (this.halLock)
/*     */             {
/* 336 */               response = this.hal.getSentence(message);
/*     */             }
/*     */ 
/* 339 */             this.pesterCount = 0;
/* 340 */             this.lastSpokeTime = System.currentTimeMillis();
/*     */           }
/*     */           else
/*     */           {
/* 344 */             synchronized (this.halLock)
/*     */             {
/* 346 */               response = this.hal.getSentence("");
/*     */             }
/* 348 */             this.pesterCount = 0;
/* 349 */             this.lastSpokeTime = System.currentTimeMillis();
/*     */           }
/* 351 */           writeToChannel(thisChannel, response);
/*     */         }
/* 353 */         this.writer.flush();
/*     */       }
/* 358 */       else if (this.learnFromChat)
/*     */       {
/* 360 */         synchronized (this.halLock)
/*     */         {
/* 363 */           if (!this.ignoreList.contains(senderNick))
/* 364 */             trained = this.hal.add(message);
/*     */           else
/* 366 */             logger.debug("Ignored message from " + sender);
/*     */         }
/* 368 */         if (trained) {
/* 369 */           trainedOnLine(message);
/*     */         }
/*     */       }
/*     */ 
/* 373 */       newLineReceived("Sender: " + sender + " Channel: " + thisChannel + " Message: " + message);
/*     */     }
/*     */     else
/*     */     {
/* 378 */       newLineReceived("Sender: " + sender + " Message: " + line.substring(endOfSender));
/*     */     }
/*     */   }
/*     */ 
/*     */   private void executeCommand(String command, String sender)
/*     */   {
/* 384 */     System.out.println("RECEIVED COMMAND: " + command);
/* 385 */     String commandName = "";
/* 386 */     String argument = "";
/* 387 */     if (command.indexOf(" ") > 0)
/*     */     {
/* 389 */       commandName = command.substring(0, command.indexOf(" "));
/* 390 */       argument = command.substring(commandName.length() + 1);
/*     */     }
/*     */     else
/*     */     {
/* 394 */       commandName = command;
/*     */     }
/* 396 */     newLineReceived("Command: " + commandName + " Argument: " + argument);
/* 397 */     if (commandName.compareToIgnoreCase("NICK") == 0) {
/* 398 */       changeNick(argument);
/* 399 */     } else if (commandName.compareToIgnoreCase("JOIN") == 0) {
/* 400 */       joinChannel(argument);
/* 401 */     } else if (commandName.compareToIgnoreCase("DIE") == 0) {
/* 402 */       killBot();
/* 403 */     } else if (commandName.compareToIgnoreCase("HELP") == 0)
/*     */     {
/* 405 */       writeToChannel(sender, "Commands are: HELP | JOIN <CHANNEL> | NICK <NICK> | SPEAK | QUIET | VERSION | LOADIGNORE | LEASH <TIME> | DIE \r\n");
/*     */     }
/* 407 */     else if (commandName.compareToIgnoreCase("VERSION") == 0)
/*     */     {
/* 409 */       writeToChannel(sender, version);
/*     */     }
/* 411 */     else if (commandName.compareToIgnoreCase("QUIET") == 0) {
/* 412 */       this.canTalk = false;
/* 413 */     } else if (commandName.compareToIgnoreCase("SPEAK") == 0)
/*     */     {
/* 415 */       this.canTalk = true;
/* 416 */       this.hisMastersVoice = false;
/*     */     }
/* 418 */     else if (commandName.compareToIgnoreCase("LOADIGNORE") == 0) {
/* 419 */       this.hal.loadIgnoreList(new File("ignore.txt"));
/* 420 */     } else if (commandName.compareToIgnoreCase("HISMASTERSVOICE") == 0)
/*     */     {
/* 422 */       this.canTalk = false;
/* 423 */       this.hisMastersVoice = true;
/*     */     }
/* 425 */     else if (commandName.compareToIgnoreCase("LEASH") == 0)
/*     */     {
/* 427 */       long newTime = 0L;
/* 428 */       if (argument.length() > 0)
/*     */       {
/*     */         try {
/* 431 */           newTime = Long.decode(argument).longValue();
/*     */         }
/*     */         catch (NumberFormatException nfe)
/*     */         {
/* 435 */           newTime = 15000L;
/*     */         }
/* 437 */         this.leashTime = newTime;
/*     */       }
/*     */ 
/*     */     }
/* 441 */     else if ((commandName.compareToIgnoreCase("SAY") == 0) && (this.hisMastersVoice))
/*     */     {
/* 443 */       String channel = argument.substring(0, argument.indexOf(" "));
/* 444 */       String message = argument.substring(channel.length() + 1);
/* 445 */       writeToChannel(channel, message);
/*     */     }
/*     */   }
/*     */ 
/*     */   private int connectToServer(String server, String channel)
/*     */     throws IOException
/*     */   {
/*     */     try
/*     */     {
/* 464 */       this.socket = new Socket(server, 6667);
/*     */     }
/*     */     catch (Exception e)
/*     */     {
/* 468 */       logger.error(e);
/* 469 */       return -1;
/*     */     }
/* 471 */     if (this.socket == null)
/*     */     {
/* 473 */       newLineReceived("Failed to connect.");
/* 474 */       return -1;
/*     */     }
/*     */ 
/* 477 */     newLineReceived("Connected successfully.");
/*     */ 
/* 479 */     this.writer = new BufferedWriter(new OutputStreamWriter(this.socket.getOutputStream()));
/* 480 */     this.reader = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
/*     */ 
/* 483 */     this.writer.write("NICK " + this.nick + "\r\n");
/* 484 */     this.writer.write("USER " + this.nick + " 8 * :talking dog\r\n");
/* 485 */     this.writer.flush();
/*     */ 
/* 489 */     String line = null;
/* 490 */     while ((line = this.reader.readLine()) != null)
/*     */     {
/* 492 */       newLineReceived(line);
/* 493 */       if (line.indexOf("004") >= 0)
/*     */       {
/* 496 */         newLineReceived("Logged in.");
/* 497 */         break;
/*     */       }
/* 499 */       if (line.indexOf("433") < 0) {
/*     */         continue;
/*     */       }
/* 502 */       System.out.println("Nickname already in use.");
/*     */ 
/* 504 */       this.nick = generateNick();
/* 505 */       this.writer.write("NICK " + this.nick + "\r\n");
/* 506 */       this.writer.flush();
/*     */     }
/*     */ 
/* 510 */     joinChannel(channel);
/*     */ 
/* 512 */     return 0;
/*     */   }
/*     */ 
/*     */   String generateNick()
/*     */   {
/* 522 */     Random random = new Random();
/* 523 */     String name = "Dog" + String.valueOf(random.nextInt());
/* 524 */     return name;
/*     */   }
/*     */ 
/*     */   private void changeNick(String newName)
/*     */   {
/* 533 */     if (newName != null)
/*     */     {
/* 535 */       if (newName.length() > 12)
/*     */       {
/*     */         try
/*     */         {
/* 539 */           this.writer.write("PRIVMSG " + this.channel + " :That name's too long, boss!\r\n");
/* 540 */           this.writer.flush();
/*     */         }
/*     */         catch (Exception e)
/*     */         {
/* 544 */           logger.error(e);
/*     */         }
/* 546 */         return;
/*     */       }
/*     */       try
/*     */       {
/* 550 */         this.writer.write("NICK " + newName + "\r\n");
/* 551 */         this.writer.flush();
/* 552 */         this.nick = newName;
/*     */       }
/*     */       catch (Exception e)
/*     */       {
/* 556 */         logger.error(e);
/*     */       }
/*     */     }
/*     */   }
/*     */ 
/*     */   private void joinChannel(String channelToJoin)
/*     */   {
/*     */     try {
/* 564 */       this.writer.write("JOIN " + channelToJoin + "\r\n");
/* 565 */       this.writer.flush();
/* 566 */       this.activeChannelList.add(channelToJoin);
/*     */     }
/*     */     catch (Exception e)
/*     */     {
/* 570 */       logger.error(e);
/*     */     }
/*     */   }
/*     */ 
///*     */   private void changeServer(String server)
///*     */   {
///* 580 */     this.server = server;
///* 581 */     killBot();
///*     */   }
/*     */ 
/*     */   private void killBot()
/*     */   {
/* 590 */     logger.info("killing bot.");
/*     */     try
/*     */     {
/* 593 */       this.socket.close();
/*     */     }
/*     */     catch (IOException e)
/*     */     {
/* 597 */       logger.error(e);
/*     */     }
/*     */   }
/*     */ 
/*     */   private void writeToChannel(String channel, String message)
/*     */   {
/*     */     try {
/* 604 */       this.writer.write("PRIVMSG " + channel + " :" + message + "\r\n");
/* 605 */       this.writer.flush();
/* 606 */       logger.debug("Wrote to " + channel + ": " + message);
/*     */     } catch (IOException e) {
/* 608 */       logger.error(e);
/*     */     }
/*     */   }
/*     */ 
/*     */   private void newLineReceived(String line)
/*     */   {
/* 615 */     for (ChatbotListener listener : this.chatListeners)
/* 616 */       listener.newLineReceived(line);
/* 617 */     logger.debug(line);
/*     */   }
/*     */ 
/*     */   private void trainedOnLine(String line)
/*     */   {
/* 622 */     for (ChatbotListener listener : this.chatListeners)
/*     */     {
/* 624 */       listener.trainedOnLine(line);
/*     */     }
/*     */   }
/*     */ }

/* Location:           C:\workspace\dognuts\dognuts\dognuts.jar
 * Qualified Name:     org.dognuts.hal.Chatbot
 * JD-Core Version:    0.6.0
 */